<template>
  <div class="app-container">
    <div class="mb-20">
      <!-- <el-button class="btn-active" icon="el-icon-download" size="mini">下载</el-button> -->
      <el-button
        class="btn-delete"
        icon="el-icon-delete"
        size="mini"
        @click="deleteRow"
        >删除</el-button
      >
    </div>
    <div class="search">
      <el-form ref="form" :model="form" label-width="80px" inline>
        <el-form-item label="">
          <el-date-picker
            v-model="form.createTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="生成时间"
          />
        </el-form-item>

        <el-form-item label="">
          <el-input v-model="form.fileName" placeholder="报表名称" />
        </el-form-item>
        <el-form-item label="">
          <el-input v-model="form.sceneName" placeholder="测试场景" />
        </el-form-item>
        <el-form-item label="">
          <el-button size="mini" icon="el-icon-search" @click="search"
            >查询</el-button
          >
          <el-button size="mini" icon="el-icon-refresh-right" @click="search(1)"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="报表名称">
        <template slot-scope="scope">
          {{ scope.row.fileName }}
        </template>
      </el-table-column>
      <el-table-column label="报表信息描述" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.fileDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="测试场景" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.sceneName }}
        </template>
      </el-table-column>
      <el-table-column label="生成时间" width="130" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="created_at"
        label="操作"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            class="detail"
            icon="el-icon-download"
            @click="download(scope.row)"
            >下载</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { fileList } from "@/api/table";
import { removeReport, downLoadReportlFile } from "@/api/form";
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      currentPage: 1,
      pageNum: 10,
      total: 0,
      form: {},
      list: null,
      listLoading: true,
      form: {},
      currentPage: 1,
      pageNum: 10,
      total: 0,
      multipleSelection: [],
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    download(row) {
      window.location.href =
        location.origin + "/file/manager/downLoadFile?fileId=" + row.fileId;
      // downLoadReportlFile({ fileId: row.fileId }).then((res) => {
      //   console.log(res);
      // });
    },
    deleteRow() {
      removeReport({
        ids: this.multipleSelection.map((item) => item.fileId).join(","),
      }).then((res) => {
        if (res.code == 200) {
          this.$message.success("操作成功");
          this.fetchData();
        }

        console.log(res);
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.pageNum = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    search(num) {
      if (num == 1) {
        this.form = {};
      }
      this.currentPage = 1;
      this.fetchData();
    },
    fetchData() {
      fileList({
        pageSize: this.pageNum,
        pageNum: this.currentPage,
        ...this.form,
      }).then((res) => {
        this.list = res.rows;
        this.total = res.total;
        this.listLoading = false;
      });
    },
  },
};
</script>
